{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['迈', '向', '充', '满', '希', '望', '的', '新', '世', '纪'],\n",
       " [0, 2, 0, 2, 0, 2, 3, 0, 1, 2])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "#工具方法,把句子转换为字序列和状态序列的函数\n",
    "def line_to_words_and_labels(line):\n",
    "\n",
    "    #状态序列\n",
    "    labels = []\n",
    "\n",
    "    #首先把一个句子去除头尾空格,以空格分割为单词\n",
    "    words = line.strip().split()\n",
    "\n",
    "    #遍历所有单词\n",
    "    for i in range(len(words)):\n",
    "\n",
    "        #把每一个字都转换成状态\n",
    "        #状态0：词语的开头\n",
    "        #状态1：一个词语的中间词\n",
    "        #状态2：一个词语的结果\n",
    "        #状态3：单个字符\n",
    "\n",
    "        #如果长度为1，则状态是3，即单个词\n",
    "        if len(words[i]) == 1:\n",
    "            labels.append(3)\n",
    "            continue\n",
    "\n",
    "        #否则，开头是状态0，结尾是状态2，中间是状态1\n",
    "        labels.append(0)\n",
    "        for t in range(len(words[i]) - 2):\n",
    "            labels.append(1)\n",
    "        labels.append(2)\n",
    "\n",
    "    #把单词的数组,转换为字的数组\n",
    "    flat = []\n",
    "    for i in words:\n",
    "        for j in i:\n",
    "            flat.append(j)\n",
    "    words = flat\n",
    "\n",
    "    return words, labels\n",
    "\n",
    "\n",
    "line_to_words_and_labels('迈向  充满  希望  的  新世纪')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "init\n",
      "A= [[0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]]\n",
      "B= [[0. 0. 0. ... 0. 0. 0.]\n",
      " [0. 0. 0. ... 0. 0. 0.]\n",
      " [0. 0. 0. ... 0. 0. 0.]\n",
      " [0. 0. 0. ... 0. 0. 0.]]\n",
      "pi= [0. 0. 0. 0.]\n",
      "count\n",
      "A= [[     0.  85897. 499329.      0.]\n",
      " [     0.  45378.  85897.      0.]\n",
      " [282224.      0.      0. 299226.]\n",
      " [290911.      0.      0. 218532.]]\n",
      "B= [[0. 0. 0. ... 0. 0. 0.]\n",
      " [0. 0. 0. ... 0. 0. 0.]\n",
      " [0. 0. 0. ... 0. 0. 0.]\n",
      " [0. 0. 0. ... 0. 0. 0.]]\n",
      "pi= [12091.     0.     0.  6963.]\n",
      "probability\n",
      "A= [[-24.79267884  -1.91884919  -0.1587329  -24.79267884]\n",
      " [-23.2979751   -1.06226695  -0.42414546 -23.2979751 ]\n",
      " [ -0.7228239  -24.78620573 -24.78620573  -0.66432584]\n",
      " [ -0.56030059 -24.65399872 -24.65399872  -0.84638552]]\n",
      "B= [[-24.79267996 -24.79267996 -24.79267996 ... -24.79267996 -24.79267996\n",
      "  -24.79267996]\n",
      " [-23.2979801  -23.2979801  -23.2979801  ... -23.2979801  -23.2979801\n",
      "  -23.2979801 ]\n",
      " [-24.79267996 -24.79267996 -24.79267996 ... -24.79267996 -24.79267996\n",
      "  -24.79267996]\n",
      " [-24.68354869 -24.68354869 -24.68354869 ... -24.68354869 -24.68354869\n",
      "  -24.68354869]]\n",
      "pi= [ -0.45481568 -21.3679578  -21.3679578   -1.00666664]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "class HMM():\n",
    "    #初始化参数\n",
    "    def __init__(self):\n",
    "        self.A = np.zeros((4, 4))\n",
    "        self.B = np.zeros((4, 65536))\n",
    "        self.pi = np.zeros(4)\n",
    "\n",
    "    #统计训练文件中,状态转换的次数,输出的次数,pi的次数\n",
    "    def count(self):\n",
    "\n",
    "        #读取训练文件\n",
    "        for line in open('text.txt', encoding='utf-8').readlines():\n",
    "\n",
    "            #每一行转换为字序列和状态序列\n",
    "            words, labels = line_to_words_and_labels(line)\n",
    "\n",
    "            #首先计算A矩阵,也就是状态转换的次数\n",
    "            for i in range(len(labels)):\n",
    "\n",
    "                #对于第一个状态来说,他不存在转换,他就是被初始化的次数,也就是pi\n",
    "                if i == 0:\n",
    "                    #取第一个字的状态,增加pi的值\n",
    "                    self.pi[labels[0]] += 1\n",
    "                    continue\n",
    "\n",
    "                #给A矩阵里,前一个状态到后一个状态转换的次数+1\n",
    "                self.A[labels[i - 1], labels[i]] += 1\n",
    "\n",
    "            #计算B矩阵,他是状态到输出的次数\n",
    "            for i in range(len(labels)):\n",
    "\n",
    "                #给B里某个状态输出某个字的次数+1\n",
    "                #ord是把字转换为一个数字\n",
    "                self.B[labels[i], ord(words[i])] += 1\n",
    "\n",
    "    def count_to_probability(self):\n",
    "\n",
    "        #为了避免0的情况,这里做一个平滑\n",
    "        self.A += 1e-5\n",
    "        self.B += 1e-5\n",
    "        self.pi += 1e-5\n",
    "\n",
    "        #先做分母,概率等于次数/总数\n",
    "        #求概率后因为是个小数,后面在连乘的时候,会导致积是个特别小的数\n",
    "        #所以在这里取对数,因为是小数,取出来肯定是个负数\n",
    "        fenmu = np.sum(self.pi)\n",
    "        for i in range(len(self.pi)):\n",
    "            self.pi[i] = np.log(self.pi[i] / fenmu)\n",
    "\n",
    "        for i in range(len(self.A)):\n",
    "            fenmu = np.sum(self.A[i])\n",
    "            for j in range(len(self.A[i])):\n",
    "                self.A[i, j] = np.log(self.A[i, j] / fenmu)\n",
    "\n",
    "        for i in range(len(self.B)):\n",
    "            fenmu = np.sum(self.B[i])\n",
    "            for j in range(len(self.B[i])):\n",
    "                self.B[i, j] = np.log(self.B[i, j] / fenmu)\n",
    "\n",
    "\n",
    "hmm = HMM()\n",
    "print('init')\n",
    "print('A=', hmm.A)\n",
    "print('B=', hmm.B)\n",
    "print('pi=', hmm.pi)\n",
    "\n",
    "hmm.count()\n",
    "print('count')\n",
    "print('A=', hmm.A)\n",
    "print('B=', hmm.B)\n",
    "print('pi=', hmm.pi)\n",
    "\n",
    "hmm.count_to_probability()\n",
    "print('probability')\n",
    "print('A=', hmm.A)\n",
    "print('B=', hmm.B)\n",
    "print('pi=', hmm.pi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "init\n",
      "delta= [[0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]]\n",
      "psi= [[0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]]\n",
      "hidden_sequence= []\n",
      "init_delta_and_psi\n",
      "delta= [[ -9.06174125 -30.85042634 -32.24981611 -11.08624695]\n",
      " [-20.37242504 -19.10208317 -15.90444812 -17.90432444]\n",
      " [-23.69642642 -30.56310789 -28.91416262 -29.73938718]\n",
      " [-37.11764833 -33.71145054 -31.74128622 -37.92079787]\n",
      " [-39.34360716 -42.60752821 -44.32430758 -43.96679537]\n",
      " [-54.3103426  -49.61352346 -46.05107644 -54.0325602 ]\n",
      " [-56.22501323 -57.97220856 -56.88127302 -48.99130674]\n",
      " [-54.74218615 -64.92497068 -63.27662129 -55.2790198 ]\n",
      " [-61.57845335 -63.48324527 -63.11176928 -66.0001913 ]\n",
      " [-70.93848039 -70.56385822 -68.65218998 -75.33727843]]\n",
      "psi= [[0. 0. 0. 0.]\n",
      " [3. 0. 0. 3.]\n",
      " [2. 1. 1. 2.]\n",
      " [2. 0. 0. 2.]\n",
      " [2. 1. 1. 2.]\n",
      " [3. 0. 0. 3.]\n",
      " [2. 1. 1. 2.]\n",
      " [3. 0. 0. 3.]\n",
      " [3. 0. 0. 3.]\n",
      " [2. 0. 0. 2.]]\n",
      "hidden_sequence= []\n",
      "get_hidden_sequence\n",
      "delta= [[ -9.06174125 -30.85042634 -32.24981611 -11.08624695]\n",
      " [-20.37242504 -19.10208317 -15.90444812 -17.90432444]\n",
      " [-23.69642642 -30.56310789 -28.91416262 -29.73938718]\n",
      " [-37.11764833 -33.71145054 -31.74128622 -37.92079787]\n",
      " [-39.34360716 -42.60752821 -44.32430758 -43.96679537]\n",
      " [-54.3103426  -49.61352346 -46.05107644 -54.0325602 ]\n",
      " [-56.22501323 -57.97220856 -56.88127302 -48.99130674]\n",
      " [-54.74218615 -64.92497068 -63.27662129 -55.2790198 ]\n",
      " [-61.57845335 -63.48324527 -63.11176928 -66.0001913 ]\n",
      " [-70.93848039 -70.56385822 -68.65218998 -75.33727843]]\n",
      "psi= [[0. 0. 0. 0.]\n",
      " [3. 0. 0. 3.]\n",
      " [2. 1. 1. 2.]\n",
      " [2. 0. 0. 2.]\n",
      " [2. 1. 1. 2.]\n",
      " [3. 0. 0. 3.]\n",
      " [2. 1. 1. 2.]\n",
      " [3. 0. 0. 3.]\n",
      " [3. 0. 0. 3.]\n",
      " [2. 0. 0. 2.]]\n",
      "hidden_sequence= [0.0, 2.0, 0.0, 2.0, 0.0, 2.0, 3.0, 3.0, 0.0, 2]\n"
     ]
    }
   ],
   "source": [
    "class Viterbi():\n",
    "    def __init__(self, A, B, pi, line):\n",
    "        self.A = A\n",
    "        self.B = B\n",
    "        self.pi = pi\n",
    "        self.line = line\n",
    "\n",
    "        #delta中存放的是每个时刻,由每个状态输出结果的概率\n",
    "        #以delta[0,0]举例,指的是时刻0由状态0输出结果的可能性\n",
    "        self.delta = np.zeros((len(line), 4))\n",
    "\n",
    "        #psi中存储的是最大可能路径\n",
    "        #以psi[1,0]举例,指的是时刻1,最有可能转移到状态0的是时刻0的哪一个状态\n",
    "        self.psi = np.zeros((len(line), 4))\n",
    "\n",
    "        #隐藏状态链\n",
    "        self.hidden_sequence = []\n",
    "\n",
    "    def init_delta_and_psi(self):\n",
    "        #在时刻0,由状态0,输出结果0的概率是 pi[0] * B[0,结果0]\n",
    "        #因为之前概率已经被转换为对数了,所以这里乘号变成了加号\n",
    "        self.delta[0, 0] = self.pi[0] + self.B[0, ord(self.line[0])]\n",
    "        self.delta[0, 1] = self.pi[1] + self.B[1, ord(self.line[0])]\n",
    "        self.delta[0, 2] = self.pi[2] + self.B[2, ord(self.line[0])]\n",
    "        self.delta[0, 3] = self.pi[3] + self.B[3, ord(self.line[0])]\n",
    "\n",
    "        #从时刻1到最后\n",
    "        for t in range(1, len(self.line)):\n",
    "            for i in range(4):\n",
    "                #时刻t转移到状态a的概率\n",
    "                to_a = np.zeros(4)\n",
    "\n",
    "                #在时刻t,由状态i输出结果的概率=上一个时刻,由状态0输出的概率*状态0转移到状态i的概率\n",
    "                #对数,乘法变加法\n",
    "                to_a[0] = self.delta[t - 1, 0] + self.A[0, i]\n",
    "                to_a[1] = self.delta[t - 1, 1] + self.A[1, i]\n",
    "                to_a[2] = self.delta[t - 1, 2] + self.A[2, i]\n",
    "                to_a[3] = self.delta[t - 1, 3] + self.A[3, i]\n",
    "\n",
    "                #时刻t由状态i输出的概率=转移到状态a的最大概率 * B[状态i,输出t]\n",
    "                #对数,乘法变加法\n",
    "                self.delta[t, i] = to_a.max() + self.B[i, ord(self.line[t])]\n",
    "\n",
    "                #在时刻t,最有可能转移到状态i的是上一个时刻的哪一个状态\n",
    "                self.psi[t, i] = to_a.argmax()\n",
    "\n",
    "    def get_hidden_sequence(self):\n",
    "\n",
    "        #最后一个时刻,最有可能输出结果的状态是x\n",
    "        max_a_to_next_a = self.delta[-1].argmax()\n",
    "        self.hidden_sequence.append(max_a_to_next_a)\n",
    "\n",
    "        #从后往前遍历,取最大可能的状态路径\n",
    "        for t in range(len(self.line) - 1, 0, -1):\n",
    "\n",
    "            #在倒数第二个时刻,最有可能转移到状态x的,是哪个状态\n",
    "            max_a_to_next_a = self.psi[t, int(max_a_to_next_a)]\n",
    "\n",
    "            #将状态放入列表中\n",
    "            self.hidden_sequence.append(max_a_to_next_a)\n",
    "\n",
    "        #逆序\n",
    "        self.hidden_sequence.reverse()\n",
    "\n",
    "\n",
    "viterbi = Viterbi(hmm.A, hmm.B, hmm.pi, '迈向充满希望的新世纪')\n",
    "print('init')\n",
    "print('delta=', viterbi.delta)\n",
    "print('psi=', viterbi.psi)\n",
    "print('hidden_sequence=', viterbi.hidden_sequence)\n",
    "\n",
    "viterbi.init_delta_and_psi()\n",
    "print('init_delta_and_psi')\n",
    "print('delta=', viterbi.delta)\n",
    "print('psi=', viterbi.psi)\n",
    "print('hidden_sequence=', viterbi.hidden_sequence)\n",
    "\n",
    "viterbi.get_hidden_sequence()\n",
    "print('get_hidden_sequence')\n",
    "print('delta=', viterbi.delta)\n",
    "print('psi=', viterbi.psi)\n",
    "print('hidden_sequence=', viterbi.hidden_sequence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'迈向|充满|希望|的|新|世纪|'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#分词\n",
    "def cut_line(line, hidden_sequence):\n",
    "\n",
    "    cut = ''\n",
    "    #遍历该行每一个字\n",
    "    for i in range(len(line)):\n",
    "        #在列表中放入该字\n",
    "        cut += line[i]\n",
    "        #如果该字是3:单个词  或  2:结尾词 ，则在该字后面加上分隔符 |\n",
    "        if (hidden_sequence[i] == 3 or hidden_sequence[i] == 2):\n",
    "            cut += '|'\n",
    "    return cut\n",
    "\n",
    "\n",
    "cut_line(viterbi.line, viterbi.hidden_sequence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "迈向|充满|希望|的|新|世纪|——|一九九八年|新年|讲话|（|附|图片|１张|）|\n",
      "中共|中央|总书记|、|国家|主席|江|泽民|\n",
      "（|一九九七年|十二月|三十|一日|）|\n",
      "１２月|３|１日|，|中共|中央|总书记|、|国家|主席|江|泽民|发表|１９９８年|新年|讲话|《|迈向|充满|希望|的|新|世纪|》|。|（|新华社|记者|兰|红光|摄|）|\n",
      "同胞|们|、|朋友|们|、|女士|们|、|先|生们|：|\n",
      "在|１９９８年|来临|之际|，|我十分|高兴|地|通过|中央|人民|广播|电台|、|中国|国际|广播|电台|和|中央|电视台|，|向|全国|各族|人民|，|向|香港|特别|行政区|同胞|、|澳门|和|台湾|同胞|、|海外|侨胞|，|向|世界|各国|的|朋友|们|，|致以|诚挚|的|问候|和|良好|的|祝愿|！|\n",
      "１９９７年|，|是|中国|发展|历史|上|非常|重要|的|很|不|平凡|的|一年|。|中国|人民|决心|继承|邓|小平|同志|的|遗志|，|继续|把|建设|有|中国|特色|社会|主义|事业|推向|前进|。|中国|政府|顺利|恢复|对|香港|行使|主权|，|并|按照|“|一国|两制|”|、|“|港人|治港|”|、|高度|自治|的|方针|保持|香港|的|繁荣|稳定|。|中国|共产|党|成功|地|召开|了|第十五次|全国|代表|大会|，|高举|邓|小平|理论|伟大|旗帜|，|总结|百年|历史|，|展望|新|的|世纪|，|制定|了|中国|跨|世纪|发展|的|行动|纲领|。|\n",
      "在|这|一年|中|，|中国|的|改革|开放|和|现代化|建设|继续|向|前|迈进|。|国民|经济|保持|了|“|高|增长|、|低|通胀|”|的|良好|发展|态势|。|农业|生产|再|次|获得|好|的|收成|，|企业|改革|继续|深化|，|人民|生活|进一步|改善|。|对外|经济|技术|合作|与|交流|不断|扩大|。|民主法制|建设|、|精神|文明|建设|和|其|他|各项|事业|都|有|新|的|进展|。|我们|十分|关注|最|近|一个|时期|一些|国家|和|地区|发生|的|金融|风波|，|我们|相信|通过|这些|国家|和|地区|的|努力|以及|有关|的|国际|合作|，|情况|会|逐步|得|到|缓解|。|总|的|来|说|，|中国|改革|和|发展|的|全局|继续|保持|了|稳定|。|\n",
      "在|这|一年|中|，|中国|的|外交|工作|取得|了|重要|成果|。|通过|高层|互访|，|中国|与|美国|、|俄罗斯|、|法国|、|日本|等|大国|确定|了|双方|关系|未来|发展|的|目标|和|指导|方针|。|中国|与|周边|国家|和|广大|发展|中|国家|的|友好|合作|进一步|加强|。|中国|积极|参与|亚太|经合|组织|的|活动|，|参加|了|东盟|—|中日|韩|和|中国|—|东盟|首脑|非|正式|会晤|。|这些|外交|活动|，|符合|和平|与|发展|的|时代|主题|，|顺应|世界|走|向|多|极化|的|趋势|，|对|于|促进|国际|社会|的|友好|合作|和|共同|发展|作出|了|积极|的|贡献|。|\n",
      "１９９８年|，|中国|人民|将|满怀|信心|地开|创新|的|业绩|。|尽管|我们|在|经济|社会|发展|中|还|面临|不少|困难|，|但|我们|有|邓|小平|理论|的|指引|，|有|改革|开放|近２０年|来|取得|的|伟|大成|就|和|积累|的|丰富|经验|，|还|有|其|他|的|各种|有利|条件|，|我们|一定|能够|克服|这些|困难|，|继续|稳步|前进|。|只要|我们|进一步|解放|思想|，|实事求|是|，|抓住|机遇|，|开拓|进取|，|建设|有|中国|特色|社会|主义|的|道路|就|会越|走越|宽广|。|\n",
      "实现|祖国|的|完|全统|一|，|是|海内外|全体|中国|人|的|共同|心愿|。|通过|中|葡|双方|的|合作|和|努力|，|按照|“|一国|两制|”|方针|和|澳门|《|基本法|》|，|１９９９年|１２月|澳门|的|回归|一定|能够|顺利|实现|。|\n",
      "台湾|是|中国|领土|不|可|分割|的|一|部分|。|完成|祖国|统一|，|是|大势|所趋|，|民心|所|向|。|任何|企图|制造|“|两|个|中国|”|、|“|一|中|一台|”|、|“|台湾|独立|”|的|图谋|，|都|注定|要|更|失败|。|希望|台湾|当局|以|民族|大义|为重|，|拿|出|诚意|，|采取|实际|的|行动|，|推动|两岸|经济|文化|交流|和|人员|往来|，|促进|两岸|直接|通邮|、|通航|、|通商|的|早日|实现|，|并|尽早|回应|我们|发出|的|在|一个|中国|的|原则|下|两岸|进行|谈判|的|郑重|呼吁|。|\n",
      "环顾|全球|，|日益|密切|的|世界|经济|联系|，|日新|月异|的|科技|进步|，|正|在|为|各国|经济|的|发展|提供|历史|机遇|。|但|是|，|世界|还|不|安宁|。|南北|之间|的|贫富|差距|继续|扩大|；|局部|冲|突时|有|发生|；|不|公正|不|合理|的|旧|的|国际|政治|经济|秩序|还|没有|根本|改变|；|发展|中|国家|在|激烈|的|国际|经济|竞争|中|仍|处于|弱势|地位|；|人类|的|生存|与|发展|还面|临种|种|威胁|和|挑战|。|和平|与|发展|的|前景|是|光明|的|，|２１|世纪|将|是|充满|希望|的|世纪|。|但|前进|的|道路|不会|也|不|可能|一帆|风顺|，|关键|是|世界|各国|人民|要|进一|步团|结起|来|，|共同|推动|早日|建立|公正|合理|的|国际|政治|经济|新|秩序|。|\n",
      "中国|政府|将|继续|坚持|奉行|独立|自主|的|和|平外|交政策|，|在|和|平共处|五|项|原则|的|基础|上|努力|发展|同|世界|各国|的|友好|关系|。|中国|愿意|加强|同联|合国|和|其|他|国际|组织|的|协调|，|促进|在|扩大|经贸|科技|交流|、|保护|环境|、|消除|贫困|、|打击|国际|犯罪|等|方面|的|国际|合作|。|中国|永远|是|维护|世界|和平|与|稳定|的|重要|力量|。|中国|人民|愿|与|世界|各国|人民|一道|，|为|开创|持久|和平|、|共同|发展|的|新|世纪|而|不懈|努力|！|\n",
      "在|这辞|旧|迎新|的|美好|时刻|，|我祝|大家|新年|快乐|，|家庭|幸福|！|\n",
      "谢谢|！|（|新华社|北京|１２月|３|１日|电|）|\n",
      "在|十五大|精神|指引|下胜|利前|进——元旦献|辞\n",
      "我们|即|将|以|丰收|的|喜悦|送|走|牛年|，|以|昂扬|的|斗志|迎来|虎年|。|我们|伟大|祖国|在|新|的|一年|，|将|是|充满|生机|、|充满|希望|的|一年|。|\n",
      "刚刚|过去|的|一年|，|大气磅礴|，|波澜|壮阔|。|在|这|一年|，|以江|泽民|同志|为|核心|的|党|中央|，|继承|邓|小平|同志|的|遗志|，|高举|邓|小平|理论|的|伟大|旗帜|，|领导|全党|和|全国|各族|人民|坚定|不|移地|沿着|建设|有|中国|特色|社会|主义|道路|阔步|前进|，|写|下|了|改革|开放|和|社会|主义|现代化|建设|的|辉煌|篇章|。|顺利|地|恢复|对|香港|行使|主权|，|胜利|地|召开|党|的|第十五次|全国|代表|大会———|两件|大事|办得|圆满|成功|。|国民|经济|稳中求进|，|国家|经济|实力|进一步|增强|，|人民|生活|继续|改善|，|对外|经济|技术|交流|日益|扩大|。|在|国际|金融|危机|的|风浪|波及|许多|国家|的|情况|下|，|我国|保持|了|金融|形势|和|整个|经济|形势|的|稳定|发展|。|社会|主义|精神|文明|建设|和|民主法制|建设|取得|新|的|成绩|，|各项|社会|事业|全面|进步|。|外交|工作|取得|可喜|的|突破|，|我国|的|国际|地位|和|国际|威望|进一步|提高|。|实践|使亿|万|人民|对|邓|小平|理论|更加|信仰|，|对|以江|泽民|同志|为|核心|的|党|中央|更加|信赖|，|对|伟大|祖国|的|光辉|前景|更加|充满|信心|。|\n"
     ]
    }
   ],
   "source": [
    "#测试\n",
    "i = 0\n",
    "for line in open('text.txt', encoding='utf-8').readlines():\n",
    "    i += 1\n",
    "    if i == 20:\n",
    "        break\n",
    "    line = line.strip().replace(' ', '')\n",
    "    v = Viterbi(hmm.A, hmm.B, hmm.pi, line)\n",
    "    v.init_delta_and_psi()\n",
    "    v.get_hidden_sequence()\n",
    "    print(cut_line(v.line, v.hidden_sequence))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
